package com.aspire.curator.api;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;

/**
 * Curator客户端框架 事务测试
 *
 * @author JustryDeng
 * @date 2018/11/22 17:24
 */
public class TranDemo {

    /**
     * 每3秒重连一次 移动重连3次
     */
    private static RetryPolicy retryPolicy = new RetryNTimes(3, 1000);

    public static void main(String[] args) throws Exception {

        CuratorFramework client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181")
                //会话超时时间
                .sessionTimeoutMs(5000)
                //重连机制 超时之后 3秒进行一次重连
                .retryPolicy(retryPolicy)
                //命名空间
                .namespace("create")
                .build();
//        CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 5000,
//                5000, retryPolicy);
        // 启动客户端
        client.start();


        // 其中creatingParentsIfNeeded()的作用为:如果要创建的节点的父节点不存在，那么会先创建父节点，再创建该节点
        client
                //事务
                .inTransaction()
                .create().forPath("/tra11", "123456".getBytes())
                .and()
                .create().forPath("/tra22", "654321".getBytes())
                .and()
                .commit();

        System.out.println("over");
    }


}